<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回一个 Directory 类实例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.closedir.html">« closedir</a></li>
      <li style="float: right;"><a href="function.getcwd.html">getcwd »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.dir.html">目录函数函数</a></li>
    <li>返回一个 Directory 类实例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.dir" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dir</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">dir</span> &mdash; <span class="dc-title">返回一个 Directory 类实例</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.dir-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>dir</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$directory</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">resource</span><span class="type"></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type"><a href="class.directory.html" class="type Directory">Directory</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  
  <p class="para rdfs-comment">
   以面向对象的方式访问目录。打开 
   <code class="parameter">directory</code> 参数指定的目录。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.dir-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">directory</code></dt>

    <dd>

     <p class="para">
      被打开的目录
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">context</code></dt>

    <dd>

      <p class="para"><a href="stream.contexts.html" class="link">上下文流（context stream）</a>
<span class="type">resource</span>。</p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.dir-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回 <span class="classname"><a href="class.directory.html" class="classname">Directory</a></span> 的实例，出现错误时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.dir-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">context</code> 现在可为空（nullable）。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.dir-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2127">
    <p><strong>示例 #1 <span class="function"><strong>dir()</strong></span> 示例</strong></p>
    <div class="example-contents"><p>
     请特别注意下面示例中 <span class="function"><a href="directory.read.html" class="function">Directory::read()</a></span> 函数返回值的判断方式。
     我们严格测试（值相等，并且类型相同，请参考 <a href="language.operators.comparison.html" class="link">
     比较运算符</a> ）返回值等于 <strong><code>false</code></strong> ，因为有些情况下，目录名可能&quot;等于&quot; <strong><code>false</code></strong> ，导致
     跳出循环。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dir</span><span style="color: #007700">(</span><span style="color: #DD0000">"/etc/php5"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Handle:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">handle&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Path:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">path&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />while&nbsp;(</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">!==&nbsp;(</span><span style="color: #0000BB">$entry&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">read</span><span style="color: #007700">()))&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$entry</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.dir-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    目录条目返回的顺序依赖于系统。
   </p>
  </p></blockquote>
 </div>

</div></div></div></body></html>